package com.part2.action.common.form;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

public class FileValueObjectAssembler implements java.io.Serializable {

	// private static final Log
	// mLog=LogFactory.getLog(FileValueObjectAssembler.class);

	/**
	 * A HashMap that stores all value objects
	 * 
	 * @uml.property name="mValues"
	 * @uml.associationEnd multiplicity="(0 -1)" ordering="ordered"
	 *                     elementType="java.lang.String" qualifier=
	 *                     "pKey:java.lang.String com.jebey.infoman.control.FileValueObject"
	 */
	private HashMap mValues;

	/**
	 * A HashMap that stores all value object list
	 * 
	 * @uml.property name="mValueLists"
	 * @uml.associationEnd multiplicity="(0 1)" qualifier=
	 *                     "pKey:java.lang.String com.jebey.infoman.control.FileValueObject"
	 */
	private HashMap mValueLists;

	public FileValueObjectAssembler() {
		mValues = new HashMap();
		mValueLists = new HashMap();
	}

	public void reset() {
		mValues.clear();
		mValueLists.clear();
	}

	public void copy(FileValueObjectAssembler pValueObjectAssembler) {
		if (pValueObjectAssembler != null) {
			mValues = pValueObjectAssembler.getValues();
			mValueLists = pValueObjectAssembler.getValueLists();
		}
	}

	public void add(FileValueObjectAssembler pValueObjectAssembler) {
		if (pValueObjectAssembler != null) {
			mValues.putAll(pValueObjectAssembler.getValues());
			mValueLists.putAll(pValueObjectAssembler.getValueLists());
		}
	}

	/**
	 * Gets a value object by its key
	 * 
	 * @param pKey
	 * @return a value object
	 */
	public FileValueObject getValue(String pKey) {
		FileValueObject vo = (FileValueObject) mValues.get(pKey);
		if (vo == null) {
			vo = new FileValueObject();
			setValue(pKey, vo);
		}
		return vo;
	}

	/**
	 * 
	 * @uml.property name="mValues"
	 */
	public HashMap getValues() {
		return mValues;
	}

	/**
	 * 
	 * @uml.property name="mValues"
	 */
	public void setValues(HashMap pValues) {
		mValues = pValues;
	}

	/**
	 * 
	 * @uml.property name="mValueLists"
	 */
	public HashMap getValueLists() {
		return mValueLists;
	}

	/**
	 * 
	 * @uml.property name="mValueLists"
	 */
	public void setValueLists(HashMap pValueLists) {
		mValueLists = pValueLists;
	}

	public void setValue(String pKey, FileValueObject pValue) {
		mValues.put(pKey, pValue);
	}

	/**
	 * Gets value object list by its key
	 * 
	 * @param pKey
	 * @return
	 */
	public FileValueObject[] getList(String pKey) {
		return (FileValueObject[]) mValueLists.get(pKey);
	}

	public void setList(String pKey, FileValueObject[] pValueList) {
		mValueLists.put(pKey, pValueList);
	}

	public void setList(String pKey, ArrayList pValueList) {
		mValueLists.put(pKey, getValueObjects(pValueList));
		/*
		 * ValueObject listAttributes = new ValueObject();
		 * listAttributes.setField("SIZE",(new
		 * Integer(pValueList.size())).toString());
		 * mValues.put(pKey+"_ATTR",listAttributes);
		 */}

	private FileValueObject[] getValueObjects(ArrayList pList) {
		if (pList == null) {
			return null;
		}

		FileValueObject[] mList = new FileValueObject[pList.size()];

		for (int i = 0; i < mList.length; i++) {
			mList[i] = (FileValueObject) pList.get(i);
		}
		return mList;
	}

	public String toString() {
		StringBuffer ret = new StringBuffer();
		Iterator keys = mValues.keySet().iterator();
		ret.append("\nValues:");
		while (keys.hasNext()) {
			String key = (String) keys.next();
			ret.append("\n" + key + ":" + getValue(key));
		}
		keys = mValueLists.keySet().iterator();
		ret.append("\nValueLists:");
		while (keys.hasNext()) {
			String key = (String) keys.next();
			ret.append("\n" + key + ":");
			FileValueObject[] vos = getList(key);
			if (vos != null) {
				for (int i = 0; i < vos.length; i++) {
					ret.append("\n" + vos[i]);
				}
			}
		}
		return ret.toString();
	}
}